#include "nrf_process.h"
#include "nrf_port.h"
#include "gzll.h"
#include "gzp.h"
#include "pair_process.h"
#include "gui_guider.h"

nrf_para_t nrf_para;

void Nrf24L01_EXTI_Line11_Disable(void)
{
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    /* 禁用EXTI Line11中断 */
    EXTI_InitStructure.EXTI_Line = EXTI_Line11;
    EXTI_InitStructure.EXTI_LineCmd = DISABLE;
    EXTI_Init(&EXTI_InitStructure);

    /* 禁用对应的NVIC中断 */
    NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
    NVIC_Init(&NVIC_InitStructure);
}


void Nrf24L01_EXTI_Line2_Disable(void)
{
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    /* 禁用EXTI Line11中断 */
    EXTI_InitStructure.EXTI_Line = EXTI_Line2;
    EXTI_InitStructure.EXTI_LineCmd = DISABLE;
    EXTI_Init(&EXTI_InitStructure);

    /* 禁用对应的NVIC中断 */
    NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
    NVIC_Init(&NVIC_InitStructure);
}


void EXTI2_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_Line2) == SET) {
        gzll_radio_isr_function();
        Nrf_Rev_Handle();
        EXTI_ClearITPendingBit(EXTI_Line2);
    }
}

/**
 * @brief NRF24L01功能引脚初始化
 */
static void Nrf24L01_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	EXTI_InitTypeDef  EXTI_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

    /* CS片选*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_SetBits(GPIOB, GPIO_Pin_12);   // 片选默认拉高，拉低发送数据，拉高停止

    /* CE*/
	GPIO_InitStructure.GPIO_Pin = CE_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(CE_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(CE_PORT, CE_PIN);

    /* IRQ*/
	GPIO_InitStructure.GPIO_Pin = IRQ_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING/*GPIO_Mode_IPU*/;
	GPIO_Init(IRQ_PORT, &GPIO_InitStructure);

    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource2); /* 将PB11配置为外部中断输入源*/

	EXTI_InitStructure.EXTI_Line = EXTI_Line2;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /* 抢占优先级*/
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;       /* 子优先级*/
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void Nrf_Enter_Pair(void)
{
    if (!nrf_para.pair_flag) {
        gzll_init();
        gzp_init();
        gzll_goto_idle();
        TimeOut_Record(&nrf_para.tout.pair_tout, PAIR_TIMEOUT);
        nrf_para.pair_flag = 1;
    }
}

void Nrf_Exit_Pair(void)
{
    if (nrf_para.pair_flag) {
        Pair_Info_Init();
        gzll_goto_idle();
        nrf_para.pair_flag = 0;
    }
}

uint8_t Get_Nrf_Pair_State(void)
{
    return nrf_para.pair_flag;
}

void Pair_Long_Handle(void)
{
    if (nrf_para.long_press_pair && (++nrf_para.long_press_time >= PRESS_LONG_PAIR)) {
        ui_load_scr_animation(&guider_ui, &guider_ui.enter_pair, guider_ui.enter_pair_del, NULL, setup_scr_enter_pair, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        Nrf_Enter_Pair();
    } else if (!nrf_para.long_press_pair) {
        nrf_para.long_press_time = 0;
    }
}


/**
 * @brief 累加和校验
 *
 * @param buf 指针地址
 * @param len 长度
 * @return uint8_t
 */
uint8_t Calc_CheckSum(uint8_t *buf, uint8_t len)
{
    uint8_t cs = 0, i;
    for (i = 0; i < len; i++) {
        cs += *buf++;
    }
    return cs;
}

/**
 * @brief 发送数据给控制盒
 *
 * @param val 命令值
 */
void Nrf_Send_Msg_Handle(uint32_t val)
{
    uint8_t buf[7];
    buf[0] = 0xA5;
    buf[1] = val>>24;
    buf[2] = val>>16;
    buf[3] = val>>8;
    buf[4] = val;
    buf[5] = Calc_CheckSum(&buf[0], 5);
    buf[6] = 0xFB;
    if (GZLL_IDLE == gzll_get_state()) {
        gzll_tx_data(buf, 7, HAL_NRF_PIPE0);
    }
}

/**
 * @brief 接收数据处理
 */
void Nrf_Rev_Handle(void)
{
    if (!nrf_para.pair_flag) {
        if (gzll_rx_fifo_read(&nrf_para.nrf.rev_buf[0], &nrf_para.nrf.rev_len, &nrf_para.nrf.rev_pipe)) {
            if (HAL_NRF_PIPE0 == nrf_para.nrf.rev_pipe && 7 == nrf_para.nrf.rev_len) {
                if (Calc_CheckSum(&nrf_para.nrf.rev_buf[0], 5) == nrf_para.nrf.rev_buf[5]) {
                    nrf_para.nrf.rev_cmd =    ((uint32_t)nrf_para.nrf.rev_buf[1])<<24 |\
                                        ((uint32_t)nrf_para.nrf.rev_buf[2])<<16 |\
                                        ((uint32_t)nrf_para.nrf.rev_buf[3])<<8  |\
                                        ((uint32_t)nrf_para.nrf.rev_buf[4])<<0;
                    backlight.sta = nrf_para.nrf.rev_cmd;
                }
            }
        }
    }
}

void Nrf_Process_Init(void)
{
    memset(&nrf_para, 0, sizeof(nrf_para_t));
    nrf_para.pair_flag = 0;
    Nrf24L01_Init();
    Spi2_Config();
    gzll_init();
    gzp_init();
    Pair_Info_Init();
    gzll_goto_idle();
    TimeOut_Record(&nrf_para.tout.send_tout, 20);
}

void Nrf_Process(void)
{
    if (nrf_para.pair_flag) {
        /* 请求主机地址成功，用新地址*/
        if (gzp_address_req_send(&pair_para.info.addr[0])) {
            pair_para.info.sta = 0xaa;
            Pair_Info_Set(&pair_para.buf[0], 6);
            gzp_update_radio_params(&pair_para.info.addr[0]);
            gzll_goto_idle();
            TimeOut_Record(&nrf_para.tout.send_tout, 20);
            nrf_para.pair_flag = 0;
            Pair_Txt_Sta(&guider_ui, 1);
            nrf_para.pair_ui_back = 1;
            TimeOut_Record(&nrf_para.tout.pair_ui_back_tout, UI_BACK_TIMEOUT);
            return;
        } else {
            /* 请求主机地址失败，用旧地址*/
            if (TimeOut_Det_Check(&nrf_para.tout.pair_tout)) {
                Pair_Info_Init();
                gzll_goto_idle();
                TimeOut_Record(&nrf_para.tout.send_tout, 20);
                nrf_para.pair_flag = 0;
                Pair_Txt_Sta(&guider_ui, 0);
                nrf_para.pair_ui_back = 1;
                TimeOut_Record(&nrf_para.tout.pair_ui_back_tout, UI_BACK_TIMEOUT);
                return;
            }
        }

    } else {
        if (nrf_para.nrf.start_send_flag) {
            if (TimeOut_Det_Check(&nrf_para.tout.send_tout)) {
                Nrf_Send_Msg_Handle(nrf_para.nrf.send_cmd);
                TimeOut_Record(&nrf_para.tout.send_tout, 20);
            }
        } else if (nrf_para.nrf.stop_send_flag) {
            if (!TimeOut_Det_Check(&nrf_para.tout.release_tout)) {
                if (TimeOut_Det_Check(&nrf_para.tout.send_tout)) {
                    Nrf_Send_Msg_Handle(0);
                    TimeOut_Record(&nrf_para.tout.send_tout, 20);
                }
            } else {
                nrf_para.nrf.stop_send_flag = 0;
                gzll_goto_idle();
            }
        }
    }
}


